package edu.zju.gis.dldsj.service.impl;

import edu.zju.gis.dldsj.dao.UserDataMapper;
import edu.zju.gis.dldsj.dao.UserDataVisitMapper;
import edu.zju.gis.dldsj.entity.DataVisit;
import edu.zju.gis.dldsj.entity.UserData;
import edu.zju.gis.dldsj.entity.vo.UserDataWithVisit;
import edu.zju.gis.dldsj.service.UserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanlo yanlong_lee@qq.com
 * @version 1.0 2018/08/09
 */
@Service
public class UserDataServiceImpl implements UserDataService {
    @Autowired
    private UserDataMapper userDataMapper;
    @Autowired
    private UserDataVisitMapper userDataVisitMapper;

    @Override
    public UserData select(String pk) {
        return userDataMapper.selectByPrimaryKey(pk);
    }

    @Override
    public int insert(UserData userData) {
        return userDataMapper.insertSelective(userData);
    }

    @Override
    public void update(UserData userData) {
        userDataMapper.updateByPrimaryKeySelective(userData);
    }

    @Override
    public void delete(String s) {
        userDataMapper.deleteByPrimaryKey(s);
    }

    @Override
    public boolean isExist(String s) {
        return userDataMapper.selectByPrimaryKey(s) != null;
    }

    @Override
    public List<UserData> getByPage(int offset, int size) {
        return userDataMapper.selectByPage(offset, size);
    }

    @Override
    public int getTotalSize() {
        return userDataMapper.selectCount();
    }

    @Override
    public int getTotalSize(String userId) {
        return userDataMapper.selectCountByUser(userId);
    }

    @Override
    public List<UserData> getByUser(String userId, int offset, int size) {
        return userDataMapper.selectByUser(userId, offset, size);
    }

    @Override
    public int insert(DataVisit dataVisit) {
        return userDataVisitMapper.insertSelective(dataVisit);
    }

    @Override
    public void update(DataVisit dataVisit) {
        userDataVisitMapper.updateByPrimaryKeySelective(dataVisit);
    }

    @Override
    public void delete(int dataVisitId) {
        userDataVisitMapper.deleteByPrimaryKey(dataVisitId);
    }

    @Override
    public DataVisit getVisit(String dataId) {
        return userDataVisitMapper.getByDataId(dataId);
    }

    @Override
    public List<DataVisit> getVisit(List<String> dataIdList) {
        return userDataVisitMapper.getByDataIdList(dataIdList);
    }

    @Override
    public List<UserDataWithVisit> getByVisitAsc(String userId, int offset, int size) {
        List<UserData> userDataList = userDataMapper.selectByUser(userId, 0, Integer.MAX_VALUE);
        return userDataList.stream().map(userData -> new UserDataWithVisit(userData, userDataVisitMapper.getByDataId(userData.getId())))
                .sorted(Comparator.comparingInt(a -> a.getVisit().getVisitCount())).skip(offset).limit(size).collect(Collectors.toList());
    }

    @Override
    public List<UserDataWithVisit> getByVisitDesc(String userId, int offset, int size) {
        List<UserData> userDataList = userDataMapper.selectByUser(userId, 0, Integer.MAX_VALUE);
        return userDataList.stream().map(userData -> new UserDataWithVisit(userData, userDataVisitMapper.getByDataId(userData.getId())))
                .sorted(Comparator.comparingInt(dataWithVisit -> -dataWithVisit.getVisit().getVisitCount())).skip(offset).limit(size).collect(Collectors.toList());
    }
}
